//
//  BGNetworkTools.swift
//  WeiboSwift
//
//  Created by 战场原黑仪 on 16/7/22.
//  Copyright © 2016年 战场原黑仪. All rights reserved.
//

import UIKit
import AFNetworking

enum BGNetworkToolsType: String {
    case GET = "GET"
    case POST = "POST"
}

class BGNetworkTools: AFHTTPSessionManager {
    //MARK: - 单例
    static let shareTools: BGNetworkTools = {
        let tools = BGNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
        
    }()
    
    //MARK: - GET,POST封装
    func resquest(type: BGNetworkToolsType, URLString: String, parameters: AnyObject?, success: (response: AnyObject?) -> Void, failure: (error: NSError) -> ()) {
//        print(parameters)
        if type == .GET {
            self.GET(URLString, parameters: parameters, progress: nil, success: { (_, response) in
                
                success(response: response)
                
                }, failure: { (_, error) in
                    
                    failure(error: error)
            })
        }else if type == .POST {
            self.POST(URLString, parameters: parameters, progress: nil, success: { (_, response) in
                
                success(response: response)
                
                }, failure: { (_, error) in
                    
                    failure(error: error)
            })
        }
    }
}

//MARK: - 请求OAuth界面数据
extension BGNetworkTools{
    
    //获取授权过的Access Token
    func oauthLoadAccessToken(code: String, success: (response: AnyObject?)->(), failure: (error: NSError)->()){
        
        let url = "https://api.weibo.com/oauth2/access_token"
        
        //拼接请求参数
        let dict: [String: AnyObject] = [
            "client_id": APPKEY,
            "client_secret": APPSECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": APPREDICRECT_URI
        ]
        
        self.resquest(.POST, URLString: url, parameters: dict, success: success, failure: failure)
        
    }
    
    //获取用户信息
    func oauthLoadUserInfo(userAccount: BGUserAccountModel, success: (response: AnyObject?)->(), failure: (error: NSError)->()) {
    
        // 请求的urlString
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        //请求参数,因为前面有做判断,所以肯定有值
        let dict = [
            "access_token": userAccount.access_token!,
            "uid": userAccount.uid!
        ]
        
        self.resquest(.GET, URLString: urlString, parameters: dict, success: success, failure: failure)
        
    }
}


//MARK: - 首页微博界面数据请求
extension BGNetworkTools {
    
    func homeLoadData(sinceId: Int64, maxId: Int64, success: (response: AnyObject?)->(), failure: (error: NSError)->()) {
        //设置url
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        //设置请求参数
        let dict = [
            "access_token": BGUserAccountViewModel.sharedTools.accessToken!,
            "since_id":"\(sinceId)",
            "max_id":"\(maxId)"
        ]
        resquest(.GET, URLString: urlString, parameters: dict, success: success, failure: failure)
        
    }
    
    
}